define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
                defOperateStatus:[
                    {id: "1", name: "启用", showIds: ["0"]},
                    {id: "0", name: "暂停", showIds: ["1"]}
                ]
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{campaignId:"campaignId",promotionPlanId:"spread",startDate:"startTime",endDate:"endTime"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.creative.pageList(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",name:"name",status:"status",statusName:"statusName",strategyCount:"strategyCount",
                            pv:"pv",click:"click",cr:"cr",price:"cpc",cost:"cost"
                        },true);
                        newInfo.status=newInfo.status.toString();
                        $.ExtCommon.conversionEmptyZero(newInfo,["pv","click","cost","cr","price"]);
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger("bind:PageList");
            });
        },
        getModalList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{promotionPlanId:"spreadId",spaceId:"spaceId"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.creative.modalList(param.campaignId,newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        return $.ExtCommon.getMappingJson(info,{
                            id:"id",name:"name",statusName:"statusName",interactionType:"type"
                        },true);
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataModalList: res});
                self.trigger("bind:ModalList");
            });
        },
        updateStatus: function (param) {
            var self = this;
            var newParam=$.ExtCommon.getMappingJson(param, {creativeId:"id",campaignId:"campaignId",status:"status"});
            servers.creative.updateStatus(newParam, function (res) {
                var newJson={};
                if(res.success){
                    newJson={status:param.status};
                    newJson.statusName=param.status.toString()==="1"?"有效":"暂停中";
                }
                res.data=newJson;
                self.set({dataUpdStatus: {req:param,res:res}});
                self.trigger("update:Status");
            });
        },
        getInfo: function (id) {
            var self = this;
            if (id) {
                servers.creative.info(id, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success ? res.data : {}, {
                        id: "id",name: "name",spread: "promotionPlanId",description: "description",objMatterType: "markType",adFormat:"adFormat",
                        targetUrl: "targetUrl",apkName:"appName",apkId:"appId",channelPack:"subordinateId",
                        space:"space",materialList:"materials"
                    }, true);
                    newJson.adFormat=newJson.adFormat.toString();
                    newJson.isNative=newJson.adFormat==="5";
                    newJson.objMatterType=newJson.objMatterType.toString();
                    var mapMatterType=servers.base.getMapObjMatterType(newJson.objMatterType);
                    newJson.isOrdinary=mapMatterType.idMap==="1";
                    newJson.isAndroid=mapMatterType.idMap==="2";
                    newJson.isIos=mapMatterType.idMap==="3";
                    newJson.isWeChat=mapMatterType.idMap==="4";
                    newJson.matterTypeFilter=mapMatterType.filterType;
                    if(newJson.isOrdinary){
                        newJson.apkName="";
                        newJson.apkId="";
                        newJson.channelPack="";
                        newJson.appId="";
                        newJson.downloadUrl="";
                    }
                    else if(newJson.isAndroid){
                        newJson.downloadUrl=newJson.targetUrl;
                        newJson.appId="";
                        newJson.targetUrl="";
                    }
                    else if(newJson.isIos){
                        newJson.appId=newJson.apkId;
                        newJson.downloadUrl=newJson.targetUrl;
                        newJson.apkId="";
                        newJson.targetUrl="";
                    }
                    else if(newJson.isWeChat){
                        newJson.apkName="";
                        newJson.apkId="";
                        newJson.channelPack="";
                        newJson.appId="";
                        newJson.downloadUrl="";
                    }
                    newJson.space=$.ExtCommon.isCheckEmpty(newJson.space)?{}:$.ExtCommon.getMappingJson(newJson.space, {
                        id:"id",name:"name",adFormat:"adFormatDesc",describe:"description",media:"mediaName", platform:"osTypeDesc"
                    }, true);
                    newJson.spaceId=newJson.space.id;
                    if($.ExtCommon.isCheckEmpty(newJson.materialList)){newJson.materialList=[]}
                    newJson.materialJson={};
                    newJson.materialList=$.map(newJson.materialList,function (info) {
                        var materialInfo= $.ExtCommon.getMappingJson(info, {
                            id: "id", tSpecId:"templateSpecId",textValue:"materialText",imgId:"uploadResourceId",urlValue:"url",groupIndex:"occurs"
                        },true);
                        materialInfo.tSpecId=materialInfo.tSpecId.toString();
                        if(newJson.isNative){
                            materialInfo.groupIndex=$.ExtCommon.isCheckEmpty(materialInfo.groupIndex)?1:materialInfo.groupIndex;
                            newJson.materialJson[materialInfo.tSpecId+"-"+materialInfo.groupIndex.toString()]=$.ExtCommon.getJsonClone(materialInfo);
                        }else{
                            newJson.materialJson[materialInfo.tSpecId]=$.ExtCommon.getJsonClone(materialInfo);
                        }
                        return materialInfo;
                    });
                    res.dataOriginal = res.data;
                    res.data = newJson;
                    self.set({dataInfo: {isAdd: false, res: res}});
                    self.trigger('bind:Info');
                });
            }else {
                var dataDef={isNative:false,materialList:[],materialJson:{}};
                self.set({dataInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:Info');
            }
        },
        addInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            servers.creative.add(data, function (res) {
                self.set({dataSave: {isAdd: true, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            data.id=json.id;
            servers.creative.update(data, function (res) {
                self.set({dataSave: {isAdd: false, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        _getMappingToServer:function(json){
            var newJson=$.ExtCommon.getMappingJson(json, {
                campaignId:"campaignId",name: "name",promotionPlanId: "spread",adFormat:"adFormat",description: "description",markType: "objMatterType",adSpaceId:"space"
            });
            var matterTypeInfo=json.matterTypeInfo;
            if(matterTypeInfo.isOrdinary){newJson.targetUrl=json.targetUrl}
            else if(matterTypeInfo.isAndroid){
                newJson.appName=json.apkName;
                newJson.appId=json.apkId;
                newJson.subordinateId=json.channelPack;
                newJson.targetUrl=json.downloadUrl;
            }
            else if(matterTypeInfo.isIos){
                newJson.appName=json.apkName;
                newJson.appId=json.appId;
                newJson.targetUrl=json.downloadUrl;
            }
            else if(matterTypeInfo.isWeChat){newJson.targetUrl=json.targetUrl}
            if(json.isNative){
                newJson.materials=[];
                $.each(json.materialList,function (i,info) {
                    $.each(info.list,function (j,cInfo) {
                        var materialInfo= $.ExtCommon.getMappingJson(cInfo, {id: "updId",templateSpecId:"tSpecId",uploadResourceId: "imgId",materialText:"textValue",occurs:"groupIndex"});
                        if(cInfo.isImage){materialInfo.materialType=1}
                        if(cInfo.isText){materialInfo.materialType=2}
                        materialInfo.templateId=json.templateId;
                        newJson.materials.push(materialInfo)
                    });
                })
            }else{
                newJson.materials=$.map(json.materialList,function (info) {
                    var materialInfo= $.ExtCommon.getMappingJson(info, {id: "updId",templateSpecId:"tSpecId",uploadResourceId: "imgId"});
                    materialInfo.templateId=json.templateId;
                    return materialInfo;
                });
            }
            return newJson;
        }
    });
});